/*  Copyright (C) 2008  Alexander Blyzniuchenko (BlezAlex)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;

namespace DC
{
    class StreamProcessor
    {
        public static void Stream2Stream(Stream source, Stream destination, long bytesCount, Task task)
        {
            byte[] buffer = new byte[1024 * 10];
            int bytesProcessed = 0;
            while (bytesProcessed < bytesCount)
            {
                int bytesReaded = source.Read(buffer, 0, (int)Math.Min(bytesCount - bytesProcessed, buffer.Length));
                if (bytesReaded == 0)
                    throw new Exception("Problems with connection. 0 bytes recived.");
                //	if (Encoding.GetEncoding(1251).GetString(buffer).Contains("$ADCSND "))
                //		System.Diagnostics.Debugger.Break();
                destination.Write(buffer, 0, bytesReaded);
                bytesProcessed += bytesReaded;
                if (bytesProcessed % (buffer.Length * 3) == 0)
                    Console.Write(".");
                task.Position += bytesReaded;
            }
            Console.WriteLine();
        }
    }
}